<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>转让用户</title>
	<%@ include file="../common/common.jsp"%>
	<link rel="stylesheet" type="text/css" href="${ctx}/page/lib/other/css/main.css" />
	<link rel="stylesheet" type="text/css" href="${ctx}/page/transfer/css/transferApply.css" />
	<link rel="stylesheet" type="text/css" href="${ctx}/page/lib/tinyalert/css/zepto.alert.css">
	<script type="text/javascript" src="${ctx}/page/lib/tinyalert/js/zepto.alert.js"></script>
</head>
<body>
<div class="line padding border-bottom">
	<div class="xs3 text-center">用户总数：</div>
	<div class="xs7 text-big text-red">${fans}</div>
</div>
<div class="line padding border-bottom">
	<div class="xs3 text-center">可转让数：</div>
	<div class="xs7"><span class="text-big text-green">${transers}</span></div>
</div>
<div class="line padding border-bottom">
	<div class="xs3 text-center">本次转让：</div>
	<div class="xs7">
		<div class="several">
			<span id="subtract" class="subtract">
				<i></i>
			</span>
			<input id="counts" class="number" type="text" value="1" />
			<span id="add" class="add">
				<i></i>
			</span>
		</div>
	</div>
</div>
<div class="line padding border-bottom">
	<div class="xs3 text-center">转让费用：</div>
	<div class="xs7"><input id="amount" class="amount" type="text" /><span class="text-small">&nbsp;贝币</span></div>
</div>
<div class=" padding-big">
	<p class="text-red">说明：</p>
	<span class="text-red text-small">
	1、可被转让的用户是指活跃用户，非活跃用户无法被转让；
	<br>
	2、只有粉丝和会员可转让用户，代理不可转让；
	<br>
	3、只有代理可以接收被转让的用户，粉丝和会员不可接收。
	</span>
</div>

<div class="margin">
	<button id="sure" class="button bg-green button-big button-block">提交转让</button>
</div>

<script type="text/javascript">
	$(document).ready(function(){
		var level = "${level}";
		var transers = "${transers}";
		$('#subtract').on('click', function() {
			var counts = $("#counts").val();
			if(parseInt(counts)<=1){

			}else{
				var newCounts = parseInt(counts) - 1;
				$("#counts").val(newCounts);
			}
		});
		$('#add').on('click', function() {
			var counts = $("#counts").val();
			if(parseInt(transers) != 0 && parseInt(transers)<=parseInt(counts)){
				$.dialog({
					content : "转让数量最大为"+transers,
					title: "alert",
					time : 2000
				});
				$("#counts").val(transers);
			}else{
				var newCounts = parseInt(counts) + 1;
				$("#counts").val(newCounts);
			}
		});
        $('#counts').on('input', function(e) {
        	var counts = $("#counts").val();
    		if(empty(counts)){
				return;
    		}
    		var re = /^[1-9]+[0-9]*]*$/;
    		if (!re.test(counts)) {
    			 counts = 1;
    			 $("#counts").val(counts);
    			 $.dialog({
    				content : '数量必须为数字!',
    				title: "alert",
    				time : 2000
    			});
    		}else if(parseInt(transers) != 0 && parseInt(transers)<parseInt(counts)){
    			counts = transers;
				$.dialog({
					content : "转让数量最大为"+transers,
					title: "alert",
					time : 2000
				});
				$("#counts").val(transers);
    		}
    		
        });
        $('#amount').on('input', function(e) {
        	var amount = $("#amount").val();
    		if(empty(amount)){
				return;
    		}
    		var re = /^[1-9]+[0-9]*]*$/;
    		if (!re.test(amount)) {
    			 $.dialog({
    				content : '费用必须为整数!',
    				title: "alert",
    				time : 2000
    			});
    			 $("#amount").val('');
    			 return;
    		}
    		var min = "${min}";
    		var max = "${max}";
    		var counts = $("#counts").val();
    		var minAmount = parseInt(counts)*parseInt(min);
    		var maxAmount = parseInt(counts)*parseInt(max);
    		if(parseInt(amount) < parseInt(minAmount)){
   			 $.dialog({
 				content : '费用最低为'+minAmount,
 				title: "alert",
 				time : 2000
 			});
 			 $("#amount").val(minAmount);
 			 return;
    		}
    		if(parseInt(amount) > parseInt(maxAmount)){
      			 $.dialog({
      				content : '费用最高为'+maxAmount,
      				title: "alert",
      				time : 2000
      			});
      			 $("#amount").val(maxAmount);
      			 return;
    		}
    		
        });
		$('#sure').on('click', function() {
			if(parseInt(level)==2){
				$.dialog({
					content : "代理不可转让用户",
					title: "alert",
					time : 2000
				});
				return;
			}
			if(parseInt(transers)<1){
				$.dialog({
					content : "可转让数量不足",
					title: "alert",
					time : 2000
				});
				return;
			}
        	var amount = $("#amount").val();
    		if(empty(amount)){
				$.dialog({
					content : "费用不能为空",
					title: "alert",
					time : 2000
				});
				return;
    		}
    		var counts = $("#counts").val();
    		if(empty(counts)){
				$.dialog({
					content : "数量不能为空",
					title: "alert",
					time : 2000
				});
				return;
    		}
    		sure(counts, amount);
		});
	});
	var flag = true;
	function sure(counts, amount){
		if(flag){
			flag = false;
			var dialog = $.dialog({
		        content : '请稍后...'
		    });
			$.ajax({
				type : 'POST',
				url : ctx + '/page/addTransferProduct.do',
				data : {
					counts:counts,
					amount:amount
				},
				dataType : 'json',
				timeout : 30000,
				context : $('body'),
				success : function(data) {
					dialog.close();
					if (data.code == "ok") {
						flag = true;
						window.location.href=mServerUrl+"/page/transferApplySuccess.htm?time="+((new Date()).getTime());
					} else {
						flag = true;
					    $.dialog({
					        content : data.msg,
					        title: "alert",
					        time : 2000
					    });
					}
				},
				error : function(xhr, type) {
					flag = true;
					dialog.close();
				    $.dialog({
				        content : '提交失败!',
				        title: "alert",
				        time : 2000
				    });
				}
			});
		}

	}

</script>
</body>
</html>